<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Variable extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('setting_m');
    }
    public function index()
    {

        $vars = $this->setting_m->get_setting_by_type();
        $data['vars'] = $vars;
        $this->load->view('variable/index', $data);
    }

    public function add()
    {

        $this->load->view('variable/form');
    }

    public function edit($k = null)
    {
        $var = $this->setting_m->get_setting_by_key($k);
        if (!$var) {
            $this->error('参数错误！');
        }
        $data['var'] = $var;
        $this->load->view('variable/form', $data);
    }

    public function del()
    {

        $k = I('get.k');
        if ($k <> '') {
            if (M('setting')->where("type=1 and k='{$k}'")->delete()) {
                addlog('删除自定义变量，ID：' . $k);
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function update()
    {

        $post = $this->input->post();
        $data['k'] = $post['k'];
        $varname = $post['varname'];
        if ($data['k'] == '') {
            $this->error('变量名不能为空。');
        }
        $is_exist = $this->setting_m->is_exist_key($data['k']);
        if ($is_exist && $varname == '') {
            $this->error('变量名称已经存在。');
        }
        $data['v'] = $post['v'];
        $data['name'] = $post['name'];
        $data['type'] = $post['type'];//自定义
        if ($varname == '') {
            $this->setting_m->add_setting($data);
        } else {
            $this->setting_m->sava_setting($varname,$data);
        }
        show_message('恭喜，操作成功！',site_url(X_URL.'variable/index'),1);
    }
}

